<?php

namespace app\models;

use think\Model;


class Money extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $updateTime = 'updated';

    /**
     * 资产变动变更记录
     * @param $user_id   // 用户
     * @param $comeuser_id   // 来源ID
     * @param $before   // 变化前资产信息
     * @param $money   // 变化金额
     * @param $cointype   // 币种类型
     * @param $handfee   // 手续费
     * @param $type   // 变化类型
     * @param $info   //备注
     * @param $source   // 资产来源ID
     * @return void
     */
    public static function MoneyEdit($before = [], $comeuser_id = 0, $money = 0, $type = 0, $info = '', $source = 0, $handfee)
    {
        if ($money != 0) {
            $money = floor($money * 10000) / 10000;
            $res = (new Money())->where(['id' => $before['id'], 'money' => $before['money']])->setInc('money', $money);
            if (!$res) {
                throw new \Exception("资产修改失败，请重试", 100);
            }
            $res2 = (new Moneylog())->save([
                'timeid' => time(),
                'user_id' => $before['user_id'],
                'comeuser_id' => $comeuser_id,
                'befores' => $before['money'],
                'money' => $money,
                'after' => ($before['money'] + $money),
                'cointype' => $before['type'],
                'type' => $type,
                'info' => $info,
                'source' => $source,
                'handfee' => $handfee
            ]);
            if (!$res2) {
                throw new \Exception("资产记录失败，请重试", 100);
            }
        }
    }
}
